var log = require('../../log.js')
import Toast from '@vant/weapp/toast/toast';
let rewardedVideoAd = null

Page({
  data: {
    title: '',
    singer: '',
    tip: '',
    userInfo: {},
    isAnonymous: false,
    disableAnonymous: true
  },
  onLoad() {
    this.setData({
      envVersion: __wxConfig.envVersion
    })
    if (this.data.envVersion == 'release') {
      if (wx.getStorageSync('userRole') >= 2) {
        console.log('等级符合 允许匿名')
        this.setData({
          disableAnonymous: false
        })
      }
      //判断是否超出点歌数量限制
      // wx.cloud.callFunction({
      //   name: 'songOrderAuth',
      // }).then(res => {
      //   console.log(res)
      //   log.info(res)
      //   if (res.result.total < 1) {
      //     console.log('songOrderAuth true')
      //   } else {
      //     console.log('songOrderAuth false')
      //     wx.showModal({
      //       title: '已达到点歌上限',
      //       content: '三天内只能点一首歌噢~',
      //       showCancel: false,
      //       success(res) {
      //         if (res.confirm) {
      //           wx.redirectTo({
      //             url: '/pages/songList/songList'
      //           })
      //         }
      //       }
      //     })
      //   }
      // })
    }
    if (wx.createRewardedVideoAd) {
      rewardedVideoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-06774433bff92124'
      })
      rewardedVideoAd.onLoad(() => {
        console.log('激励视频 广告加载成功')
      })
      rewardedVideoAd.onError((err) => {
        console.log('onError event emit', err)
      })
      rewardedVideoAd.onClose(res => {
        var time = new Date()
        if (res && res.isEnded) {
          // 正常播放结束，可以下发奖励
          wx.cloud.callFunction({
            name: 'songOrder',
            data: {
              title: this.data.title,
              singer: this.data.singer,
              tip: this.data.tip,
              time: time,
              pickerInfo: wx.getStorageSync('userInfo'),
              state: 0,
              isAnonymous: this.data.isAnonymous
            }
          }).then(res => {
            console.log(res)
            log.info(res)
          }).catch(err => {
            console.log(err)
            log.error(err)
          })
          wx.showModal({
            showCancel: false,
            title: '点歌成功',
            content: '请等待审核，并请关注公众号每周发布的点歌投票！',
            success(res) {
              log.info(res)
              if (res.confirm) {
                wx.requestSubscribeMessage({
                  tmplIds: ['mgVCvgEH75iv5CyyT32bpzF7-L2qVz9Zft8Pjux5Y0Q', 'dOWStaenGZnEJn2gqOORde51HKQQ-pOqQFgstvatytM', 'G0m_yoi3Gz7_osG_wSLVaSGxpz0DNFiE9oT1T1kD8u8'],
                  success(res) {
                    console.log('已授权接收订阅消息', res)
                    wx.reLaunch({
                      url: '/pages/songList/songList'
                    })
                  },
                  fail(err) {
                    console.log(err)
                    log.err(err)
                    wx.reLaunch({
                      url: '/pages/songList/songList'
                    })
                  }
                })
              }
            }
          })
        } else {
          // 播放中途退出，不下发奖励
          console.log('激励视频 中途退出')
          wx.showToast({
            icon: 'none',
            title: '未完成播放，点歌失败',
          })
        }
      })
    }
  },
  addSong() {
    // var that = this
    if (this.data.title == '') {
      Toast.fail('歌名为空');
    } else if (this.data.singer == '') {
      Toast.fail('歌手为空');
    } else {
      if (wx.getStorageSync('openid')) {
        wx.showModal({
          title: '点歌信息确认',
          content: '歌名：' + this.data.title + '\n歌手：' + this.data.singer,
          success(res) {
            log.info(res)
            rewardedVideoAd.show()
              .catch(() => {
                rewardedVideoAd.load()
                  .then(() => rewardedVideoAd.show())
                  .catch(err => {
                    console.log('激励视频 广告显示失败')
                  })
              })
          }
        })
      } else {
        wx.navigateTo({
          url: '/pages/user/login/login',
        })
      }
    }
  },
  switchChange(e) {
    this.setData({
      isAnonymous: e.detail.value
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: 'TLradio - 点歌',
      path: '/page/songOrder/songOrder',
      //imageUrl: '../../images/share1.png'
    }
  }
})